<?php
//无卡套现；快捷收款

namespace app\pay\service;

use think\Controller;

class Hqkjsk extends Controller
{
	protected $mchId = "jn2019032514062";//商户号
	protected $signKey = "baede106";//密钥
	protected $version = "0100";

	//快捷进件
	public function register($param = [])
	{
		if (empty($param) || !is_array($param)) {
			return false;
		}

		$data = [
			//公共参数
			"transcode" => "025",//交易码;015落地还款H;902查询接口
			"version" => $this->version,//版本号
			"merchno" => $this->mchId,//商户号


			//业务参数
			"ordersn" => $param["ordersn"],//流水号
			"dsorderid" => $param["dsorderid"],//商户订单号
			"futureRateValue" => (string)$param["futureRateValue"],//每笔费率
			"fixAmount" => (string)$param["fixAmount"],//单笔价格
			"accountName" => $param["accountName"],//真实姓名
			"idcard" => $param["idcard"],//身份证号
			"mobile" => $param["mobile"],//
			"settleBankCard" => $param["settleBankCard"],//收款银行卡号
			"settleBankName" => $param["settleBankName"],//收款银行卡名称:民生银行
			"settleBankCode" => $param["settleBankCode"],//银行代码:CMBC
		];
		//halt($data);
		return $this->unified($data, "http://pay.huanqiuhuiju.com/authsys/weishua/api/merchant/register.do");
	}


	//快捷支付
	public function pay($param)
	{
		if (empty($param) || !is_array($param)) {
			return false;
		}

		$data = [
			"transcode" => "026",//交易码;015落地还款H;902查询接口
			"version" => "0100",//版本号
			"ordersn" => $param["ordersn"],//流水号
			"dsorderid" => $param["dsorderid"],//商户订单号
			"merchno" => $this->mchId,//商户号


			"amount" => (string)$param["amount"],//金额
			"bankcard" => $param["bankcard"],//消费银行卡号
			"accountName" => $param["accountName"],//真实姓名
			"mobile" => $param["mobile"],//消费卡手机号
			"cvn2" => $param["cvn2"],//消费银行卡的cvn2
			"expireDate" => $param["expireDate"],//消费银行卡的有效期
			"subMerchantNo" => $param["subMerchantNo"],//子商户号
			"returnUrl" => $param["returnUrl"],//用来标识用户在智能设备上登陆账号ID信息的哈希值
			"notifyUrl" => $param["notifyUrl"],//异步通知地址
		];
		return $this->unified($data, "http://pay.huanqiuhuiju.com/authsys/weishua/api/quick/pay.do");
	}


	//快捷修改(卡号和费率)
	public function updateFee($param)
	{
		if (empty($param) || !is_array($param)) {
			return false;
		}

		$data = [
			"transcode" => "027",//交易码;015落地还款H;902查询接口
			"version" => $this->version,//版本号
			"ordersn" => $param["ordersn"],//流水号
			"dsorderid" => $param["dsorderid"],//商户订单号
			"merchno" => $this->mchId,//商户号
			"subMerchantNo" => $param["subMerchantNo"],//子商户号

			"futureRateValue" => (string)$param["futureRateValue"],//每笔费率
			"fixAmount" => (string)$param["fixAmount"],//单笔价格
			"settleBankCard" => $param["settleBankCard"],//收款银行卡号
			"settleBankName" => $param["settleBankName"],//收款银行卡名称:民生银行
			"settleBankCode" => $param["settleBankCode"],//收款银行卡代码:CMBC
			"accountName" => $param["accountName"],//收款人姓名
			"mobile" => $param["mobile"],//收款手机号
		];
		//halt($data);
		$res = $this->unified($data, "http://pay.huanqiuhuiju.com/authsys/weishua/api/update/fee.do");
		//halt($res);
		return $res;
	}


	//订单查询接口
	public function queryOrder($param)
	{
		if (empty($param) || !is_array($param)) {
			return false;
		}

		$data = [
			"transcode" => "902",//订单查询
			"version" => $this->version,//版本号

			"ordersn" => $param["ordersn"],//流水号
			"dsorderid" => $param["dsorderid"],//商户订单号
			"merchno" => $this->mchId,//商户号
			"transtype" => $param["transtype"],//交易类型如15
		];

		$res = $this->unified($data, "http://pay.huanqiuhuiju.com/authsys/api/auth/execute.do");
		//halt($res);
		return $res;
	}


	/**
	 * 统一下单接口
	 *
	 * @param        $data
	 * @param string $apiUrl
	 *
	 * @return bool|mixed|string
	 */
	private function unified($data, $apiUrl = "")
	{
		if (empty($data)) {
			return false;
		}
		if (empty($apiUrl)) {
			return false;
		}
		$data = $this->getPaySign($data);
		$data = json_encode($data);
		$res = $this->post_json($data, $apiUrl);
		$res = json_decode($res, true);
		return $res;
	}

	//签名
	private function getPaySign(array $data)
	{
		$tempArr = $data;
		$str = "";
		unset($tempArr['sign']);
		ksort($tempArr);
		foreach ($tempArr as $k => $v) {
			if (!empty($v)) {
				$str .= "{$k}={$v}";
			}
		}
		$sign = md5($str . $this->signKey);
		$data["sign"] = $sign;
		return $data;
	}


	/**
	 * 支付post+json提交
	 * @author QQ
	 */
	function post_json($jsonStr, $url = "")
	{
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_POST, 1);
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonStr);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch, CURLOPT_HTTPHEADER, array(
				'Content-Type: application/json; charset=utf-8',
				'Content-Length: ' . strlen($jsonStr)
			)
		);
		$response = curl_exec($ch);
		curl_close($ch);
		return $response;
	}

	private function create_order_no()
	{
		$order_no = date('Ymd') . substr(time(), -5) . substr(microtime(), 2, 5) . sprintf('%02d', rand(1000, 9999));
		return $order_no;
	}
}